package com.zkw.group.controller;

import com.alibaba.fastjson.JSON;
import com.zkw.group.entity.Course;
import com.zkw.group.entity.QueryInfo;
import com.zkw.group.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(description = "管理课程基本信息")
@CrossOrigin
@RestController
@RequestMapping("/Courses")
public class CourseController {

    @Autowired
    private CourseService courseService;


    @ApiOperation(value = "分页查询所有用户信息")
    @GetMapping("/AllCourse")
    public String getAllCourseList(QueryInfo queryInfo) {
        int numbers = courseService.getCourseCounts(queryInfo.getQuery(),queryInfo.getRole());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Course> courses = courseService.getAllCourseList(queryInfo.getQuery(),queryInfo.getRole(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",courses);
        return JSON.toJSONString(res);
    }

    @ApiOperation(value = "添加课程基础信息")
    @PostMapping("/Course")
    public String addCourse(@RequestBody Course course) {
        int result;
        try{
            result = courseService.addCourse(course);
        }
        catch(Exception e) {
            result = 0;
        }
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "根据课程id查询课程基础信息")
    @GetMapping("/CourseMessage")
    public String getUpdateCourse(String courseid) {
        Course updateCourse = courseService.getUpdateCourse(courseid);
        String users_json = JSON.toJSONString(updateCourse);
        return users_json;
    }

    @ApiOperation(value = "更新课程基础信息")
    @PutMapping("/Course")
    public String updateCourse(@RequestBody Course course) {
        int result = courseService.updateCourse(course);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "根据课程id删除课程基础信息")
    @DeleteMapping("/Course")
    public String deleteCourse(String courseid) {
        int result = courseService.deleteCourse(courseid);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "更改课程发布状态")
    @PutMapping("/CourseState")
    public String editCourseState(String state,String courseid){
        int states = courseService.updateState(state,courseid);
        String str = states >0?"success":"error";
        return str;
    }


    //------------------------------------------------课程详细内容部分----------------------------------------
    @ApiOperation(value = "根据课程id查询课程详细信息")
    @GetMapping("/CourseDetail")
    public String getCourseDetail(String courseid) {
        Course updateCourse = courseService.getCourseDetail(courseid);
        String users_json = JSON.toJSONString(updateCourse);
        return users_json;
    }

    @ApiOperation(value = "更新课程详细信息")
    @PutMapping("/CourseDetail")
    public String updateCourseDetail(@RequestBody Course course) {
        int result = courseService.updateCourseDetail(course);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "查询所有课程名称")
    @GetMapping("/AllCname")
    public String getAllCnameList(QueryInfo queryInfo) {
        int numbers = courseService.getCourseCounts(queryInfo.getQuery(),queryInfo.getRole());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Course> courses = courseService.getAllCourseList(queryInfo.getQuery(),queryInfo.getRole(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",courses);
        return JSON.toJSONString(res);
    }
}
